import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_data_center/conf/config.dart';
import 'package:flutter_data_center/screens/common/perlmisson_request_widget.dart';
import 'package:flutter_data_center/screens/common/protocol_model.dart';
import 'package:flutter_data_center/screens/common/user_helper.dart';
import 'package:flutter_data_center/screens/guide/guide_screen.dart';
import 'package:flutter_data_center/screens/home/home_screen.dart';
import 'package:flutter_data_center/screens/welcome/welcome_screen.dart';
import 'package:flutter_data_center/utills/log_utils.dart';
import 'package:flutter_data_center/utills/navigator_utils.dart';
import 'package:flutter_data_center/utills/sp_utils.dart';

import 'package:permission_handler/permission_handler.dart';

class IndexScreen extends StatefulWidget {
  static String routeName = "/index";
  @override
  _IndexScreenState createState() => _IndexScreenState();
}

class _IndexScreenState extends State<IndexScreen> with ProtocolModel {
  // 是否去设置中心
  List<String> _list = [
    "为您更好的体验应用，所以需要获取摄像机权限",
    "您已拒绝权限，所以无法保存您的一些偏好设置，将无法使用APP",
    "您已拒绝权限，请在设置中心中同意APP的权限请求",
    "其他错误"
  ];
  @override
  void initState() {
    super.initState();
    // 延时的原因是 在加载页面初始化的时候，还没有拿到context
    // 使用pageBuilder 可以获取透明背景 其他方式的背景是黑色的
    Future.delayed(Duration.zero, () {
      initData();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: ClipOval(
          child: Image.asset(
            "assets/images/app_icon.png",
            width: 50,
            height: 50,
          ),
        ),
      ),
    );
  }

  void initData() {
    // 初始化日志打印
    bool isLog = !bool.fromEnvironment("dart.vm.product");
    LogUtils.init(isLog: isLog);
    LogUtils.e("权限申请");
    NavigatorUtils.pushPageByFade(
        context: context,
        targetPage: PermissionRequestWidget(
          permission: Permission.camera,
          permissionList: _list,
        ),
        dismissCallback: (value) {
          LogUtils.e("权限申请结果： $value");
          if (value == null || !value) {
            // 权限请求不通过
            LogUtils.e("权限申请不通过");
          } else {
            // 权限请求通过
            initNextData();
          }
        });
  }

  void initNextData() async {
    // Directory tempDir = await getTemporaryDirectory();
    // String tempPath = tempDir.path;

    // Directory appDocDir = await getApplicationDocumentsDirectory();
    // String appDocPath = appDocDir.path;
    // print("tempPath $tempPath");
    // print("appDocPath $appDocPath");
    // 初始化本地缓存
    await SPUtils.init();
    await Config.init();
    bool isAgreement = await SPUtils.getBool("isAgreement");
    LogUtils.e("isAgreement $isAgreement");
    if (isAgreement == null || !isAgreement) {
      isAgreement = await showProtocolFunction(context);
    }
    // 初始化用户信息
    LogUtils.e("初始化用户信息");
    UserHelper.getInstance.init();
    if (isAgreement) {
      LogUtils.e("同意协议");
      // 保存标识 本地缓存
      SPUtils.save("isAgreement", true);
      next();
    } else {
      LogUtils.e("不同意");
      closeApp();
    }
  }

  void closeApp() {
    SystemChannels.platform.invokeMethod("SystemNavigator.pop");
  }

  void next() async {
    // 是否第一次安装
    bool isFirstInstall = await SPUtils.getBool("data_center_isFirst");

    // 引导页
    if (isFirstInstall == null || isFirstInstall == false) {
      // 倒计时页面
      NavigatorUtils.pushPageByFade(
        context: context,
        targetPage: GuideScreen(),
        isReplace: true,
      );
      await SPUtils.save("data_center_isFirst", true);
    } else {
      // 倒计时页面
      NavigatorUtils.pushPageByFade(
        context: context,
        targetPage: HomeScreen(),
        isReplace: true,
      );
    }
  }
}
